/**
 * 数据转换器
 * 将api接口返回的数据转换为前端需要的格式
 */

'use strict';

/**
 * 构建 布置作业-老师-题目数
 * @param  {array} workData API返回作业数数据
 * @param  {array} subjectData API返回题目数数据
 * @param  {array} teacherData API返回教师数数据
 * @return {object}      协议格式数据
 */
function FixupWorkNewTrend(workData, teacherData, subjectData) {
	let rn = {
		series: [],
		categories: new Set()
	};

	let data = [{
		title: '作业数',
		content: workData
	}, {
		title: '教师数',
		content: teacherData
	}, {
		title: '题目数',
		content: subjectData
	}];

	rn = data.reduce((rn, item) => {
		let arr = [];

		item.content.forEach((item) => {
			rn.categories.add(item.show_name || item.time_sheet);
			arr.push(parseFloat(item.result));
		});

		rn.series.push({
			name: item.title,
			data: arr
		});

		return rn;
	}, rn);

	rn.categories = Array.from(rn.categories);

	return rn;
}

function FixupWorkCumulativeTrend(workData, teacherData, subjectData) {
	return FixupWorkNewTrend(workData, teacherData, subjectData);
}

function FixupWorkSubjectDistribution(workData, teacherData, subjectData) {
	return FixupWorkNewTrend(workData, teacherData, subjectData);
}

function FixupWorkAreaDistribution(workData, teacherData, subjectData) {
	return FixupWorkNewTrend(workData, teacherData, subjectData);
}

function FixupWorkSubmissionRate(rateData) {
	let rn = {
		series: [],
		categories: new Set()
	};

	let arr = [];

	rateData.forEach((item) => {
		rn.categories.add(item.show_name || item.time_sheet);
		arr.push(parseFloat(item.result));
	});

	rn.series.push({
		name: '作业提交率',
		data: arr
	});

	rn.categories = Array.from(rn.categories);

	return rn;
}

/**
 * 老师布置作业-题型分布
 * @param {[type]} questionData [description]
 * @description 0 选择题 1 多选题 2 解答题 3 填空 4 翻译 5 完形 6 阅读理解 7 语法
 */
function FixupWorkQuestionsDistribution(questionData) {
	let rn = {
			series: [{
				name: '题目数目',
				data: []
			}],
			categories: ['主观题', '客观题']
		},
		subjective = '23457',
		objective = '016';

	// debugger;
	let subjectiveData = 0,
		objectiveData = 0;

	questionData.forEach((item) => {
		if (objective.indexOf(item.multi_data) != -1) {
			objectiveData += parseInt(item.result);
		} else if (subjective.indexOf(item.multi_data) != -1) {
			subjectiveData += parseInt(item.result);
		}
	});

	rn.series[0].data.splice(0, 0, subjectiveData, objectiveData);

	return rn;
}

module.exports = {
	FixupWorkNewTrend,
	FixupWorkCumulativeTrend,
	FixupWorkSubjectDistribution,
	FixupWorkAreaDistribution,
	FixupWorkSubmissionRate,
	FixupWorkQuestionsDistribution
};